package com.rongshu.view.discovery.adapter;

import android.animation.Animator;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.AnimationUtil;
import com.rongshu.utils.GlideUtils;
import com.rongshu.utils.LoginUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.discovery.bean.AuthorDetailBean;
import com.rongshu.view.discovery.contract.AuthorItemClickListener;
import com.utils.FastClickUtils;
import com.widget.loading.AnimatorListener;

import java.util.List;

/**
 * created by zhengweis on 2018/11/1.
 * description：
 */
public class AuthorListItemAdapter extends RecyclerView.Adapter<AuthorListItemAdapter.ViewHolder> {

    private Context context;
    private List<AuthorDetailBean> list;
    private AuthorItemClickListener listener;

    public void setAdapterListener(AuthorItemClickListener listener) {
        this.listener = listener;
    }

    public AuthorListItemAdapter(Context context, List<AuthorDetailBean> list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.adapter_author_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
        GlideUtils.loadImageView(context, viewHolder.logo, list.get(i).getAvatar(), R.drawable.user_head_icon, R.drawable.user_head_icon);
        viewHolder.name.setText(list.get(i).getBi_name());
        viewHolder.keepAuthor.setText(list.get(i).getIsConcern() == 0 ? "关注" : "已关注");
        viewHolder.keepIcon.setVisibility(list.get(i).getIsConcern() == 0 ? View.VISIBLE : View.GONE);
        viewHolder.keepAuthor.setTextColor(list.get(i).getIsConcern() == 0 ?
                ContextCompat.getColor(context, R.color.color_3CC051) : ContextCompat.getColor(context, R.color.color_9f9f9f));
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener != null) {
                    listener.onItemClickListener(AuthorItemClickListener.INTENT_DETAILS, i);
                }
            }
        });
        viewHolder.addAttention.setVisibility(list.get(i).getUserid() == UserInfoUtils.getUserId() ? View.GONE : View.VISIBLE);
        viewHolder.vip.setVisibility(list.get(i).getVip() ? View.VISIBLE : View.GONE);
        viewHolder.addAttention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                if (!FastClickUtils.isFastClick() && listener != null) {
                    if (UserInfoUtils.getUserInfo() == null) {
                        LoginUtils.toLogin(context);
                    } else {
                        if (list.get(i).getIsConcern() == 1) {
                            AnimationUtil.AlphaScaleShrinkAnimator(view, new AnimatorListener() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    view.animate().alpha(1).scaleX(1).scaleY(1);
                                }
                            });

                        } else {
                            AnimationUtil.AlphaScaleAnimator(view, new AnimatorListener() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    view.animate().alpha(1).scaleX(1).scaleY(1);
                                }
                            });
                        }
                        if (listener != null) {
                            listener.onItemClickListener(AuthorItemClickListener.ADD_ATTENTION, i);
                        }
                    }
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView name, keepAuthor;
        private ImageView logo, vip, keepIcon;
        private LinearLayout addAttention;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            logo = itemView.findViewById(R.id.author_logo);
            name = itemView.findViewById(R.id.author_name);
            addAttention = itemView.findViewById(R.id.layout_add_attention);
            keepIcon = itemView.findViewById(R.id.keep_author_icon);
            keepAuthor = itemView.findViewById(R.id.keep_author);
            vip = itemView.findViewById(R.id.vip);
        }
    }
}
